package com.mall.web.control.eshop;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.client.eshop.SaleEshopService;
import com.mall.client.search.EShopLuceneBuildService;
import com.mall.common.util.bit.PropertiesConfigure;
import com.mall.constant.PropertiesKey;
import com.mall.custom.dao.read.AreaReadMapper;
import com.mall.custom.dao.read.CityReadMapper;
import com.mall.custom.dao.read.ProvinceReadMapper;
import com.mall.enums.SaleEshopStatusEnum;
import com.mall.enums.SaleEshopTypeEnum;
import com.mall.pojo.Area;
import com.mall.pojo.AreaExample;
import com.mall.pojo.City;
import com.mall.pojo.CityExample;
import com.mall.pojo.Province;
import com.mall.pojo.ProvinceExample;
import com.mall.pojo.SaleEshop;
import com.mall.query.Page;
import com.mall.query.eshop.SaleEshopQuery;
import com.mall.query.search.EshopSearchQuery;
import com.mall.searchdo.SearchResultIdDO;
import com.mall.web.util.HttpUtil;

@Controller
@RequestMapping("/eshop/search/")
public class EshopSearchController {
	@Autowired
	private EShopLuceneBuildService eShopLuceneBuildService;
	@Autowired
	private PropertiesConfigure propertiesConfigure;
	@Autowired
	private CityReadMapper cityReadMapper;
	@Autowired
	private AreaReadMapper areaReadMapper;
	@Autowired
	private ProvinceReadMapper provinceReadMapper;
	@Autowired
	private SaleEshopService saleEshopService;

	@RequestMapping("eshopsearch")
	public String eshopsearch(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "eshop/eshopsearch";

	}

	@RequestMapping("eshopsearchbuildall")
	public void eshopsearchbuildall(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String eshopSearchDir = getEshopsearchbuilPath();
		SaleEshopQuery saleEshopQuery = new SaleEshopQuery();
		saleEshopQuery.setStatus(0);
		saleEshopQuery.setStatus(SaleEshopStatusEnum.ZAISHOU.getSaleEshopStatus());
		saleEshopQuery.setType(SaleEshopTypeEnum.TIANMAO.getSaleEshopType());
		saleEshopQuery.setPage(Page.getInitPage(50));
		List<SaleEshop> saleTianmaoEshopList = this.saleEshopService.getSaleEshopListBySaleEshopQuery(saleEshopQuery);
		eShopLuceneBuildService.deleteAll(eshopSearchDir);
		eShopLuceneBuildService.buildAll(saleTianmaoEshopList, eshopSearchDir);
		System.out.println("build ok");
	}

	@RequestMapping("deleteeshopsearch")
	@ResponseBody
	public void deleteeshopsearch(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		eShopLuceneBuildService.deleteAll(getEshopsearchbuilPath());
		System.out.println("deleteeshopsearch ok");
	}

	@RequestMapping("search")
	public void search(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long begin = HttpUtil.getLongValueFromRequest(request, "begin");
		Long end = HttpUtil.getLongValueFromRequest(request, "end");
		Long id = HttpUtil.getLongValueFromRequest(request, "id");
		String keyword = request.getParameter("keyword");
		String searchAll = request.getParameter("searchAll");
		EshopSearchQuery eshopSearchQuery = new EshopSearchQuery();
		if (begin != null) {
			eshopSearchQuery.setBeginPrice(begin);
		}
		if (end != null) {
			eshopSearchQuery.setEndPrice(end);
		}
		if (id != null) {
			eshopSearchQuery.setId(id);
		}
		if (StringUtils.isNotBlank(keyword)) {
			eshopSearchQuery.setKeyWord(keyword);
		}
		
		if (StringUtils.isNotBlank(searchAll)) {
			eshopSearchQuery.setSearchAll(true);
		}
		List<SearchResultIdDO> searchResultIdDOList = eShopLuceneBuildService.searchEshopFromLucene(eshopSearchQuery,
				getEshopsearchbuilPath(), 100);
		System.out.println(searchResultIdDOList);
		System.out.println("search ok");
	}

	public String getEshopsearchbuilPath() {
		return propertiesConfigure.getProperties(PropertiesKey.eshopsearchdir);
	}

	public List<SaleEshop> mockSaleEshopList() {

		AreaExample areaexample = new AreaExample();
		areaexample.setLimit(1000);
		List<Area> areaList = areaReadMapper.selectByExample(areaexample);

		CityExample cityexample = new CityExample();
		cityexample.setLimit(1000);
		List<City> cityList = cityReadMapper.selectByExample(cityexample);

		ProvinceExample provinceexample = new ProvinceExample();
		provinceexample.setLimit(1000);
		List<Province> provinceList = provinceReadMapper.selectByExample(provinceexample);

		List<SaleEshop> saleEshopList = new ArrayList<>();
		SaleEshop saleEshop = new SaleEshop();
		for (int i = 0; i < 100; i++) {
			saleEshop = new SaleEshop();
			saleEshop.setId(Long.parseLong(i + ""));
			saleEshop.setTitle(areaList.get(i).getAreaName());
			saleEshop.setChinaArea(getchinaArea(i));
			saleEshop.setProvinceName(getprovincename(provinceList, i));
			saleEshop.setCityName(cityList.get(i).getCityName());
			saleEshop.setPrice(i * 10000  );
			saleEshopList.add(saleEshop);
		}

		return saleEshopList;
	}

	public String getchinaArea(int i) {
		String[] a = new String[] { "华南", "华北", "华东", "西北", "西南" };
		int mod = i % 5;
		return a[mod];
	}

	public String getprovincename(List<Province> provinceList, int i) {
		int mod = i % 10;
		return provinceList.get(mod).getProvinceName();
	}

}
